var themeCallFn = {
    //单图风格1
    'singleImage01': {
        //设置值
        setVal: function (res, obj) {
            $(obj).find('.file-url').val(res[0].file_url);
        },
        //预览
        preview: function (obj) {
            var dom = $(obj).closest('.f-upload');
            var fileUrl = dom.find('.file-url').val();
            if (valid.isEmpty(fileUrl)) {
                helper.tips('请上传图片');
                return false;
            }
            layer.photos({
                photos: {
                    "start": 0, //初始显示的图片序号，默认0
                    "data": [   //相册包含的图片，数组格式
                        {"src": fileUrl}
                    ]
                }
            });
        },
        //删除
        del: function (obj) {
            var dom = $(obj).closest('.f-upload');
            var fileUrl = dom.find('.file-url').val();
            if (valid.isEmpty(fileUrl)) {
                helper.tips('请上传图片');
                return false;
            }
            dom.find('.file-url').val('');
        }
    },
    //多图风格1
    'multiImage01': {
        //设置
        setVal: function (res, obj) {
            var html = juicer(themeCallFn.multiImage01.tpl, {list: res});
            var uploadObj = obj.closest('.f-upload');
            obj.find('.upload-list').append(html);
            themeCallFn.multiImage01.setInputValue(uploadObj)
        },
        setInputValue: function (uploadObj) {
            var values = [];
            var itemRowObj = uploadObj.find('.item-row');
            itemRowObj.each(function (i) {
                var url = $(this).find('.file-url').val();
                values.push(url);
            });
            var name = uploadObj.data('name');
            $("input[name='" + name + "']").val(values.join(','));
        },
        //预览
        preview: function (obj) {
            var data = [];
            var start = 0;
            var selfItemRow = $(obj).closest('.item-row');
            var itemRow = $(obj).closest('.f-upload').find('.item-row');
            itemRow.removeClass('show-active');
            selfItemRow.addClass('show-active');
            itemRow.each(function (i) {
                var url = $(this).find(".file-url").val();
                if ($(this).closest('.item-row').hasClass('show-active')) start = i;
                data.push({src: url});
            });
            layer.photos({
                photos: {
                    "start": start, //初始显示的图片序号，默认0
                    "data": data
                }
            });
        },
        //上移
        upMove: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            helper.upMove(obj);
            themeCallFn.multiImage01.setInputValue(uploadObj);
        },
        //下移
        downMove: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            helper.downMove(obj);
            themeCallFn.multiImage01.setInputValue(uploadObj);
        },
        //删除
        del: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            $(obj).closest('.item-row').remove();
            themeCallFn.multiImage01.setInputValue(uploadObj);
        },
        tpl: helper.heredoc(function () {/*
        {@each list as row,index}
        <li class="item-row" style="background:url(${row.file_url}) no-repeat center center / cover;">
            <div class="icon-bar">
                <input type="hidden" class="file-url" value="${row.file_url}" />
                <i class="mdi mdi-arrow-left text-white" onclick="themeCallFn.multiImage01.upMove(this)"></i>
                <i class="mdi mdi-arrow-right text-white" onclick="themeCallFn.multiImage01.downMove(this)"></i>
                <i class="mdi mdi-delete text-white" onclick="themeCallFn.multiImage01.del(this)"></i>
                <i class="mdi mdi-eye text-white" onclick="themeCallFn.multiImage01.preview(this)"></i>
            </div>
        </li>
        {@/each}
        */})
    },
    //多图风格2
    'multiImage02': {
        //设置
        setVal: function (res, obj) {
            var html = juicer(themeCallFn.multiImage02.tpl, {list: res});
            var uploadObj = obj.closest('.f-upload');
            obj.find('.upload-list').append(html);
            themeCallFn.multiImage02.setInputValue(uploadObj)
        },
        setInputValue: function (uploadObj) {
            var values = [];
            var itemRowObj = uploadObj.find('.item-row');
            itemRowObj.each(function (i) {
                var url = $(this).find('.file-url').val();
                var s1 = $(this).find('.file-s1').val();
                var s2 = $(this).find('.file-s2').val();
                values.push({file_url:url,s1:s1,s2:s2});
            });
            var name = uploadObj.data('name');
            values = values.length==0 ? '' : JSON.stringify(values);
            $("textarea[name='" + name + "']").val(values);
        },
        //预览
        preview: function (obj) {
            var data = [];
            var start = 0;
            var selfItemRow = $(obj).closest('.item-row');
            var itemRow = $(obj).closest('.f-upload').find('.item-row');
            itemRow.removeClass('show-active');
            selfItemRow.addClass('show-active');
            itemRow.each(function (i) {
                var url = $(this).find('.file-url').val();
                if ($(this).closest('.item-row').hasClass('show-active')) start = i;
                data.push({src: url});
            });
            layer.photos({
                photos: {
                    "start": start, //初始显示的图片序号，默认0
                    "data": data
                }
            });
        },
        //上移
        upMove: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            helper.upMove(obj);
            themeCallFn.multiImage02.setInputValue(uploadObj);
            console.info(uploadObj);
        },
        //下移
        downMove: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            helper.downMove(obj);
            themeCallFn.multiImage02.setInputValue(uploadObj);
        },
        //删除
        del: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            $(obj).closest('.item-row').remove();
            themeCallFn.multiImage02.setInputValue(uploadObj);
        },
        //失去焦点
        blur:function (obj){
            var uploadObj = $(obj).closest('.f-upload');
            themeCallFn.multiImage02.setInputValue(uploadObj);
        },
        tpl: helper.heredoc(function () {/*
        {@each list as row,index}
        <li class="item-row">
            <div class="g-bd">
                <div class="g-sd">
                    <div class="image" style="background:url(${row.file_url}) no-repeat center center / cover;">
                        <div class="icon-bar">
                            <i class="mdi mdi-arrow-left text-white" onclick="themeCallFn.multiImage02.upMove(this)"></i>
                            <i class="mdi mdi-arrow-right text-white" onclick="themeCallFn.multiImage02.downMove(this)"></i>
                            <i class="mdi mdi-delete text-white" onclick="themeCallFn.multiImage02.del(this)"></i>
                            <i class="mdi mdi-eye text-white" onclick="themeCallFn.multiImage02.preview(this)"></i>
                        </div>
                    </div>
                </div>
                <div class="g-mn">
                    <div class="g-mnc">
                        <input type="hidden" class="file-url" value="${row.file_url}" />
                        <input type="text" class="form-control input-sm file-s1" onBlur="themeCallFn.multiImage02.blur(this);" value="${row.s1}" />
                        <textarea class="form-control input-sm file-s2" onBlur="themeCallFn.multiImage02.blur(this);" style="height:60px;">${row.s2}</textarea>
                    </div>
                </div>
            </div>
        </li>
        {@/each}
        */})
    },
    //单附件风格1
    'singleFile01': {
        //设置值
        setVal: function (res, obj) {
            $(obj).find('.file-url').val(res[0].file_url);
        },
        //预览
        preview: function (obj) {
            var dom = $(obj).closest('.f-upload');
            var fileUrl = dom.find('.file-url').val();
            if (valid.isEmpty(fileUrl)) {
                helper.tips('请上传附件');
                return false;
            }
            top.location = fileUrl;
        },
        //删除
        del: function (obj) {
            var dom = $(obj).closest('.f-upload');
            var fileUrl = dom.find('.file-url').val();
            if (valid.isEmpty(fileUrl)) {
                helper.tips('请上传附件');
                return false;
            }
            dom.find('.file-url').val('');
        }
    },
    //多附件风格1
    'multiFile01':{
        //设置
        setVal: function (res, obj) {
            var html = juicer(themeCallFn.multiFile01.tpl, {list: res});
            var uploadObj = obj.closest('.f-upload');
            obj.find('.upload-list').append(html);
            themeCallFn.multiFile01.setInputValue(uploadObj)
        },
        setInputValue: function (uploadObj) {
            var values = [];
            var itemRowObj = uploadObj.find('.item-row');
            itemRowObj.each(function (i) {
                var fileUrl = $(this).find('.file-url').val();
                var fileExt = $(this).find('.file-ext').val();
                var fileName = $(this).find('.file-name').val();
                values.push({file_url:fileUrl,file_ext:fileExt,file_name:fileName});
            });
            var name = uploadObj.data('name');
            values = values.length==0 ? '' : JSON.stringify(values);
            $("textarea[name='" + name + "']").val(values);
        },
        //上移
        upMove: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            helper.upMove(obj);
            themeCallFn.multiFile01.setInputValue(uploadObj);
        },
        //下移
        downMove: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            helper.downMove(obj);
            themeCallFn.multiFile01.setInputValue(uploadObj);
        },
        //删除
        del: function (obj) {
            var uploadObj = $(obj).closest('.f-upload');
            $(obj).closest('.item-row').remove();
            themeCallFn.multiFile01.setInputValue(uploadObj);
        },
        //失去焦点
        blur:function (obj){
            var uploadObj = $(obj).closest('.f-upload');
            themeCallFn.multiFile01.setInputValue(uploadObj);
        },
        tpl: helper.heredoc(function () {/*
        {@each list as row,index}
        <li class="item-row">
            <input type="hidden" class="file-ext" value="${row.file_ext}" />
            <div class="file-ext ${row.file_ext}"></div>
            <div class="input-group m-b-10">
                <span class="input-group-addon">名称</span>
                <input type="text" class="form-control file-name" onBlur="themeCallFn.multiImage02.blur(this);" value="${row.original_name}" placeholder="文件名称" />
            </div>
            <div class="input-group">
                <span class="input-group-addon">路径</span>
                <input type="text" class="form-control file-url" value="${row.file_url}" readonly />
            </div>
            <div class="icon-bar">
                <i class="mdi mdi-arrow-up" onclick="themeCallFn.multiFile01.upMove(this)"></i>
                <i class="mdi mdi-arrow-down" onclick="themeCallFn.multiFile01.downMove(this)"></i>
                <i class="mdi mdi-delete" onclick="themeCallFn.multiFile01.del(this)"></i>
            </div>
        </li>
        {@/each}
        */})
    },
    selectFile: function (e) {
        var obj = $(e).closest('.f-upload');
        var option = obj.data();
        var theme = option.theme;
        var callfn = option.callfn;
        var type = $(e).data('type') || 'single';
        var file = $(e).data('file') || '';
        var url = '/system/upload/select.html?type=' + type + '&file_type=' + file;
        var index = layer.open({
            type: 2,
            title: '选择图片',
            area: ['980px', '730px'],
            btn: ['确定'],
            content: url,
            yes: function (index, layero) {
                var iframeWin = window[layero.find('iframe')[0]['name']]; //得到iframe页的窗口对象，执行iframe页的方法：
                var list = iframeWin.doAction.subFile();
                if(!valid.isEmpty(callfn)){
                    eval(callfn + '(list,obj)');
                }else{
                    eval('themeCallFn.' + theme + '.setVal(list,obj)');
                }
                layer.close(index);
            }
        });
        var bodyWidth = $(document.body).width();
        if (bodyWidth < 768) layer.full(index);
    }
};